package org.xin.framework.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.xin.common.constant.Constants;
import org.xin.framework.mapper.*;
import org.xin.framework.pojo.entity.SysDictionary;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.xin.framework.base.service.impl.IBaseServiceImpl;
import org.xin.framework.pojo.entity.SysDictionaryItem;
import org.xin.framework.service.ISysDictionaryItemService;
import org.xin.framework.service.ISysDictionaryService;

import java.util.List;

/**
* <p>
    * 系统模块-字典表 服务实现类
    * </p>
*
* @author: xin
* @date: Create in 2021-09-29
*/
@Slf4j
@Service
public class SysDictionaryServiceImpl extends IBaseServiceImpl<SysDictionary, SysDictionaryMapper> implements ISysDictionaryService {

    @Autowired
    private ISysDictionaryItemService dictionaryItemService;

    @Override
    public List<SysDictionaryItem> selectNamesByCode(String code) {
        SysDictionary sysDictionary = selectByCode(code);
        return dictionaryItemService.selectByDictionaryId(sysDictionary.getId());
    }

    @Override
    public String selectItemNameByCode(String code) {
        String[] arr = code.split(Constants.COLON_SPLIT);
        if (arr.length != 2) {
            return null;
        }
        SysDictionary sysDictionary = selectByCode(arr[0]);

        SysDictionaryItem sysDictionaryItem = dictionaryItemService.selectNameByDictionaryIdAndCode(sysDictionary.getId(), arr[1]);

        return sysDictionary.getName() + Constants.COLON_SPLIT + sysDictionaryItem.getName();
    }

    @Override
    public SysDictionary selectByCode(String code) {
        lambdaQueryWrapper()
                .eq(SysDictionary::getCode, code);
        return baseMapper.selectOne(lambdaQueryWrapper);
    }
}
